Paul's JavaScript Examples    
 
Check an entered name

This handy script checks a name entry. If there are no spaces it is safe to assume either first or last name was not entered. if characters such as @,!,*,% and # are found, one may assume either a prankster was at work or a user filled in an e-mail address instead of a name. The script will signal all this.

Example

Please enter your full name:

Usage

<INPUT TYPE="TEXT" NAME="Name" VALUE="" SIZE="25" MAXLENGTH="25" onChange="Check_Name(this.value)">

Source

<SCRIPT LANGUAGE="javascript">
<!--
function Check_Name(item)
{
        var returnVal = false
        checkVal = 0
        spatie = 0
        for (var i=0; i < item.length; i++)
        {
                if (item.substring(i,i+1) == ' ')
                {
                        spatie = 1
                }
                if (item.substring(i,i+1) == '!' || item.substring(i,i+1) == '@' || item.substring(i,i+1) == '#' || item.substring(i,i+1) == '*' || item.substring(i,i+1) == '%')
                {
                        checkVal = 1
                }
        }
        if (checkVal == 0 && spatie == 1)  returnVal = true
        if (spatie == 0)   fout = 'You need to fill in both your first and last name.'
        if (checkVal == 1) fout = 'The name entry contains non alphabetic characters.'
        if (returnVal == false)
        {
                alert('Are you sure you entered your real name fully and correctly?\n \nError Report:\n' + fout)
        }
        return returnVal
}
// -->
</SCRIPT>